//
//  CNUIButton.swift
//  YiZhangGui
//
//  Created by 卢逢卫 on 2024/4/3.
//

import UIKit

public class CNUIButton: UIButton {
    public enum CNUIButtonType{
        case leftImgRightTitle
        case leftTitleRightImg
        case topTitleBottomImg
        case topImgBottomTitle
//        case custom(type: UIButton.ButtonType)
    }

    public var btnType: CNUIButtonType
    public var spacing: CGFloat
    
    private func commonInit() {
            self.btnType = .leftImgRightTitle
            self.spacing = 0.0
        }
    
//    public override init(frame: CGRect) {
//            super.init(frame: frame)
//            commonInit()
//        }
    
    required init?(coder: NSCoder) {
            self.btnType = .leftImgRightTitle  // 设置默认值，或者根据需求进行修改
            self.spacing = 0  // 设置默认值，或者根据需求进行修改
            super.init(coder: coder)
        }

    public init(btnType: CNUIButtonType = .leftImgRightTitle, spacing: CGFloat = 0) {
       
        self.btnType = btnType
        self.spacing = spacing
        super.init(frame: .zero)
        // 使用 .custom 类型初始化父类 UIButton
    }

//    required init?(coder: NSCoder) {
//        fatalError("init(coder:) has not been implemented")
//    }

    override public func layoutSubviews() {
        super.layoutSubviews()

        guard let imageView = self.imageView, let titleLabel = self.titleLabel else {
            return
        }

        switch btnType {
        case .leftImgRightTitle:
            imageEdgeInsets = UIEdgeInsets(top: 0, left: -spacing/2, bottom: 0, right: spacing/2)
            titleEdgeInsets = UIEdgeInsets(top: 0, left: spacing/2, bottom: 0, right: -spacing/2)
        case .leftTitleRightImg:
            imageEdgeInsets = UIEdgeInsets(top: 0, left: titleLabel.frame.size.width + spacing/2, bottom: 0, right: -titleLabel.frame.size.width - spacing/2)
            titleEdgeInsets = UIEdgeInsets(top: 0, left: -imageView.frame.size.width - spacing/2, bottom: 0, right: imageView.frame.size.width + spacing/2)
        case .topTitleBottomImg:
            imageEdgeInsets = UIEdgeInsets(top: 0, left: 0, bottom: -titleLabel.frame.size.height - spacing/2, right: -titleLabel.frame.size.width)
            titleEdgeInsets = UIEdgeInsets(top: -imageView.frame.size.height - spacing/2, left: -imageView.frame.size.width, bottom: 0, right: 0)
        case .topImgBottomTitle:
           
            imageEdgeInsets = UIEdgeInsets(top: -titleLabel.frame.size.height - spacing/2, left: 0, bottom: 0, right: -titleLabel.frame.size.width)
            titleEdgeInsets = UIEdgeInsets(top: 0, left: -imageView.frame.size.width, bottom: -imageView.frame.size.height - spacing/2, right: 0)
//        case .custom(type: let type):
//            break
//            self.buttonType = type
            
        }
    }

    override public func point(inside point: CGPoint, with event: UIEvent?) -> Bool {
        let increaseBounds = CGRect(x: bounds.origin.x - spacing, y: bounds.origin.y - spacing, width: bounds.size.width + 2 * spacing, height: bounds.size.height + 2 * spacing)
        return increaseBounds.contains(point)
    }
}
